package team.group.exam.dao.impl;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import team.group.exam.dao.IUsersDao;
import team.group.exam.entity.TbPaper;
import team.group.exam.entity.TbUser;

public class UsersDaoImpl extends HibernateDaoSupport implements IUsersDao{

	/**
	 * 分页查询每页显示的记录数
	 */
	private int pageSize;
	/**
	 * 增加一个用户
	 */
	@Override
	public void addUser(TbUser user) {
		 this.getHibernateTemplate().save(user);
	}

	/**
	 * 删除一个用户
	 */
	@Override
	public void deleteUser(TbUser user) {
		this.getHibernateTemplate().delete(user);
		
	}

	/**
	 * 更新
	 */
	@Override
	public void updateUser(TbUser user) {
		this.getHibernateTemplate().update(user);
		
	}

	/**
	 * 获取所有得到用户
	 */
	@Override
	public List<TbUser> getAllUsers() {
		List<TbUser> list = this.getHibernateTemplate().find("from TbUser");
		return list;
	}
	
	/**
	 * 获取所有得到用户
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<TbUser> getAllUsers(int currentPage) {
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		List<TbUser> list = session.createCriteria(TbUser.class)
		                           .setFirstResult((currentPage-1)*pageSize)
		                           .setMaxResults(pageSize).list();
		session.close();
		return list;
	}

	/**
	 * 根据id查询一个用户
	 */
	@Override
	public TbUser getUserById(int id) {
		List<TbUser> list = this.getHibernateTemplate().find("from TbUser where userId=?", id);
		return list.get(0);
	}

	/**
	 * 用户登录验证
	 */
	@Override
	public boolean userLogin(TbUser user) {
		List<TbUser> list = this.getHibernateTemplate().find("from TbUser where userName=? and userPassword=?", user.getUserName(),user.getUserPassword());
		if(list.size() > 0){
			return true;
		}
		return false;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 查询总页数
	 */
	@Override
	public int getTotalPage() {
		// TODO Auto-generated method stub
		List<Long> list = this.getHibernateTemplate().find("select count(userId) from TbUser");
		int count = Integer.parseInt(list.get(0).toString());
		int totalPage = (count%pageSize) == 0 ? (count/pageSize): ((count/pageSize)+1);
		return totalPage;
	}
	
	/**
	 * 试卷表中添加用户
	 * 更新试卷
	 */
	public void addUserToPaper(TbPaper paper){
		this.getHibernateTemplate().update(paper);
	}

	/**
	 * 查找该用户所关联的试卷
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<TbPaper> getUserPaper(TbUser user) {
		 List<TbPaper> list = this.getHibernateTemplate().find("from TbPaper where tbUser.userId=?", user.getUserId());
		 for(int i = 0; i < list.size(); i++){
			 TbPaper paper = list.get(i);
			 System.out.println(paper.getPaperId() + paper.getPaperName());
		 }
		 return list;
	}

	@Override
	public TbUser getcheckUser(TbUser user) {
		// TODO Auto-generated method stub
		List<TbUser> list = this.getHibernateTemplate().find("from TbUser where userName=? and userPassword=?", user.getUserName(),user.getUserPassword());
		return list.get(0);
	}
	

}
